//A valid number can be split up into these components (in order): 
//
// 
// A decimal number or an integer. 
// (Optional) An 'e' or 'E', followed by an integer. 
// 
//
// A decimal number can be split up into these components (in order): 
//
// 
// (Optional) A sign character (either '+' or '-'). 
// One of the following formats:
// 
// At least one digit, followed by a dot '.'. 
// At least one digit, followed by a dot '.', followed by at least one digit. 
// A dot '.', followed by at least one digit. 
// 
// 
// 
//
// An integer can be split up into these components (in order): 
//
// 
// (Optional) A sign character (either '+' or '-'). 
// At least one digit. 
// 
//
// For example, all the following are valid numbers: ["2", "0089", "-0.1", "+3.1
//4", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"], w
//hile the following are not valid numbers: ["abc", "1a", "1e", "e3", "99e2.5", "-
//-6", "-+3", "95a54e53"]. 
//
// Given a string s, return true if s is a valid number. 
//
// 
// Example 1: 
//
// 
//Input: s = "0"
//Output: true
// 
//
// Example 2: 
//
// 
//Input: s = "e"
//Output: false
// 
//
// Example 3: 
//
// 
//Input: s = "."
//Output: false
// 
//
// Example 4: 
//
// 
//Input: s = ".1"
//Output: true
// 
//
// 
// Constraints: 
//
// 
// 1 <= s.length <= 20 
// s consists of only English letters (both uppercase and lowercase), digits (0-
//9), plus '+', minus '-', or dot '.'. 
// 
// Related Topics 数学 字符串 
// 👍 185 👎 0


package leetcode.editor.cn;

import org.junit.Assert;

import java.util.HashMap;
import java.util.Map;

//Java：Valid Number
class P65ValidNumber {
    public static void main(String[] args) {
        Solution solution = new P65ValidNumber().new Solution();
        System.out.println(+.1e1);
        Assert.assertEquals(solution.isNumber("+.1e+"), true);
        // TO TEST
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public boolean isNumber(String s) {
            if (s == null || s.length() == 0) {
                return false;
            }
            if (s.equals(".") || s.equals("e") || s.equals("E") || s.equals("+") || s.equals("-")) {
                return false;
            }
            boolean existNum = false;
            boolean hasDot = false;
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (c == 'e' || c == 'E') {
                    if (!existNum || i == s.length() - 1) {
                        return false;
                    }
                    if (!isInteger(s.substring(i + 1))) {
                        return false;
                    } else {
                        return true;
                    }
                } else if (c == '+' || c == '-') {
                    if (i != 0) {
                        return false;
                    }
                } else if (c != '.' && (c >= '0' && c <= '9')) {
                    existNum = true;
                } else if (c == '.') {
                    if (hasDot == false) {
                        hasDot = true;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            }
            if (existNum == false) {
                return false;
            }
            return true;
        }

        public boolean isInteger(String string) {
            for (int i = 0; i < string.length(); i++) {
                char c = string.charAt(i);
                if (c == '+' || c == '-') {
                    if (i != 0) {
                        return false;
                    } else if (i == string.length() - 1) {
                        return false;
                    }
                } else if (c == '.') {
                    return false;
                } else if (!(c >= '0' && c <= '9')) {
                    return false;
                }
            }
            return true;
        }

        public boolean isNumberState(String s) {
            Map<State, Map<CharType, State>> transfer = new HashMap<State, Map<CharType, State>>();
            Map<CharType, State> initialMap = new HashMap<CharType, State>() {{
                put(CharType.CHAR_NUMBER, State.STATE_INTEGER);
                put(CharType.CHAR_POINT, State.STATE_POINT_WITHOUT_INT);
                put(CharType.CHAR_SIGN, State.STATE_INT_SIGN);
            }};
            transfer.put(State.STATE_INITIAL, initialMap);
            Map<CharType, State> intSignMap = new HashMap<CharType, State>() {{
                put(CharType.CHAR_NUMBER, State.STATE_INTEGER);
                put(CharType.CHAR_POINT, State.STATE_POINT_WITHOUT_INT);
            }};
            transfer.put(State.STATE_INT_SIGN, intSignMap);
            Map<CharType, State> integerMap = new HashMap<CharType, State>() {{
                put(CharType.CHAR_NUMBER, State.STATE_INTEGER);
                put(CharType.CHAR_EXP, State.STATE_EXP);
                put(CharType.CHAR_POINT, State.STATE_POINT);
            }};
            transfer.put(State.STATE_INTEGER, integerMap);
            Map<CharType, State> pointMap = new HashMap<CharType, State>() {{
                put(CharType.CHAR_NUMBER, State.STATE_FRACTION);
                put(CharType.CHAR_EXP, State.STATE_EXP);
            }};
            transfer.put(State.STATE_POINT, pointMap);
            Map<CharType, State> pointWithoutIntMap = new HashMap<CharType, State>() {{
                put(CharType.CHAR_NUMBER, State.STATE_FRACTION);
            }};
            transfer.put(State.STATE_POINT_WITHOUT_INT, pointWithoutIntMap);
            Map<CharType, State> fractionMap = new HashMap<CharType, State>() {{
                put(CharType.CHAR_NUMBER, State.STATE_FRACTION);
                put(CharType.CHAR_EXP, State.STATE_EXP);
            }};
            transfer.put(State.STATE_FRACTION, fractionMap);
            Map<CharType, State> expMap = new HashMap<CharType, State>() {{
                put(CharType.CHAR_NUMBER, State.STATE_EXP_NUMBER);
                put(CharType.CHAR_SIGN, State.STATE_EXP_SIGN);
            }};
            transfer.put(State.STATE_EXP, expMap);
            Map<CharType, State> expSignMap = new HashMap<CharType, State>() {{
                put(CharType.CHAR_NUMBER, State.STATE_EXP_NUMBER);
            }};
            transfer.put(State.STATE_EXP_SIGN, expSignMap);
            Map<CharType, State> expNumberMap = new HashMap<CharType, State>() {{
                put(CharType.CHAR_NUMBER, State.STATE_EXP_NUMBER);
            }};
            transfer.put(State.STATE_EXP_NUMBER, expNumberMap);

            int length = s.length();
            State state = State.STATE_INITIAL;

            for (int i = 0; i < length; i++) {
                CharType type = toCharType(s.charAt(i));
                if (!transfer.get(state).containsKey(type)) {
                    return false;
                } else {
                    state = transfer.get(state).get(type);
                }
            }
            return state == State.STATE_INTEGER || state == State.STATE_POINT || state == State.STATE_FRACTION || state == State.STATE_EXP_NUMBER || state == State.STATE_END;
        }

        public CharType toCharType(char ch) {
            if (ch >= '0' && ch <= '9') {
                return CharType.CHAR_NUMBER;
            } else if (ch == 'e' || ch == 'E') {
                return CharType.CHAR_EXP;
            } else if (ch == '.') {
                return CharType.CHAR_POINT;
            } else if (ch == '+' || ch == '-') {
                return CharType.CHAR_SIGN;
            } else {
                return CharType.CHAR_ILLEGAL;
            }
        }


    }

    enum State {
        STATE_INITIAL,
        STATE_INT_SIGN,
        STATE_INTEGER,
        STATE_POINT,
        STATE_POINT_WITHOUT_INT,
        STATE_FRACTION,
        STATE_EXP,
        STATE_EXP_SIGN,
        STATE_EXP_NUMBER,
        STATE_END,
    }

    enum CharType {
        CHAR_NUMBER,
        CHAR_EXP,
        CHAR_POINT,
        CHAR_SIGN,
        CHAR_ILLEGAL,
    }

//leetcode submit region end(Prohibit modification and deletion)

}